
faApp.controller('OutProductListCtrl',['$scope','outProductService','$location','$controller','$route','$routeParams','sessionService',function($scope,outProductService,$location,$controller,$route,$routeParams,sessionService) {

 $controller('BaseListCtrl', {$scope: $scope}); 

  $scope.showEstablishaFormFlag=false;
  $scope.showPublishFailFormFlag=false;
  $scope.config={
    "productType":"",
    "pageSize" : 20,
    "pageNo" : 1
  };
  $scope.param={
    "productType":"",
    "id":"",
    "checkStatus":"",
    "failReson":"",
    "failReson2":"",
  };
  $scope.showResonFlag=false;
  $scope.searchMap = {};
  $scope.config.productType = $routeParams.t;
  $scope.searchMap.productType = $routeParams.t;
  $scope.param.productType = $routeParams.t;

  $scope.changeStatus=function(id,status){
    if($scope.param.failReson==''&&status=='3'){
      if($scope.showEstablishaFormFlag){
        alert("原因必填!!");
      }
      $scope.pId=id;
      $scope.showEstablishaFormFlag=true;
      return;
    }else if($scope.param.failReson2==''&&status=='6'){
      if($scope.showPublishFailFormFlag){
        alert("原因必填!!");
      }
      $scope.pId=id;
      $scope.showPublishFailFormFlag=true;
      return;
    }

    if(confirm("确认此操作？")){
      $scope.param.id=id;
      $scope.param.checkStatus=status;
      if(status=='6'){
        $scope.param.failReson=$scope.param.failReson2;
      }
      outProductService.editOutProduct($scope.param).then(function(json){
        if(json.statusCode=='4002'){
         alert("产品未发布!");
       }else if (json.statusCode=='4003'){
        alert("产品未成立!");
      }else if (json.statusCode=='4003'){
        alert("出错了:" + json.statusCode);
      }else if (json.statusCode=='4004'){
        alert("供应方已被黑名单!");
      }else if (json.statusCode=='4005'){
        alert("供应方不存在!");
      }else if (json.statusCode=='4006'){
        alert("供应方未审核通过!");
      }else if (json.statusCode){
        alert("出错了:" + json.statusCode);
      }else{
       //alert("审核成功!");
       $route.reload();
     }
   });
    }
  }
  $scope.showReson=function(v){
    $scope.showResonFlag=true;
    $scope.showResonfailReson=v;
  }
  $scope.$watch('config',function(){
    outProductService.outProductList($scope.config).then(function(json){
    $scope.list = json.foregroundProductList;
   //处理rate为 json数组
    angular.forEach($scope.list,function(n){   
       if(n.rebateType=='1'){
         if(n.rebateRate.substring(0,1)!="["){
              n.rebateRebateJson=angular.fromJson("["+n.rebateRate+"]");
         }else{
          n.rebateRebateJson=angular.fromJson(n.rebateRate);
         }
       }
    });
    $scope.pager=json.pager;
    $scope.loading = false;
    });
  },true);

  $scope.showProductName=function(productType,id){
    if(id&&(productType=='02'||productType=='03')){
      return 0;
    }else if(productType=='06'&&id){
      return 1;
    }else if(productType=='05'&&id){
      return 2;
    }else {
      return 3;
    }
  }
  $scope.showProductPush=function(productType,id){
    if((productType=='02'||productType=='03')&&!id){
      return 0;
    }else if(productType=='06'&&!id){
      return 1;
    }else if(productType=='05'&&!id){
      return 2;
    }
  }

  $scope.chkProjectAll = function(){
    if($("#chkProjectAll").attr("checked") == "checked"){
      $("[name='chkProject']").attr("checked","checked");
    }else{
      $("[name='chkProject']").attr("checked",false);
    }
  }

  $scope.sendEmail=function(){
    var allCheck = $("[name='chkProject']");
    var pids = "";
    $("[name='chkProject']").each(function(){
      if($(this).attr("checked") == "checked"){  
        pids += $(this).val() + ",";
      }
    });
    if(pids.length > 0){
      if(!confirm("确认发送吗?")){
        return;
      }
      pids = pids.substring(0, pids.length - 1);
      outProductService.sendoutProductEmail(pids).then(function(json){
        if (json.statusCode){
          alert("出错了:" + json.statusCode);
        }else{
          alert("发送成功！");
          $scope.alertNextPage("发送成功！");
        }
      });
    }else{
      alert("请选择一个产品！");
    }
  }
}]);

faApp.controller('OutProductDetailCtrl',['$scope','outProductService','$location','$controller','$route','$routeParams','sessionService',function($scope,outProductService,$location,$controller,$route,$routeParams,sessionService) {
  $scope.t = $routeParams.type;
  outProductService.outProductDetail($routeParams.id).then(function(json){
    $scope.detail = json.foregroundProductDetail;
    //处理rate为 json数组
       if($scope.detail.rebateType == '1'){
         if($scope.detail.rebateRate.substring(0,1)!="["){
              $scope.detail.rebateRebateJson=angular.fromJson("["+$scope.detail.rebateRate+"]");
         }else{
          $scope.detail.rebateRebateJson=angular.fromJson($scope.detail.rebateRate);
         }
       }

  })  
}]);

faApp.controller('ProductRegexCtrl',['$scope','outProductService','$location','$controller','$route','$routeParams','sessionService',function($scope,outProductService,$location,$controller,$route,$routeParams,sessionService) {
  $scope.t = $routeParams.type;
  $scope.config={
    "pageSize" : 20,
    "pageNo" : 1
  };

  outProductService.productRegexList($scope.config).then(function(json){
    $scope.productRegexList = json.fetchXpath;
  });

  $scope.isShowEdit = '';  

  $scope.productRegexEdit=function(o){
    console.log(o);
    $scope.isShowEdit=o.id;
  }
  $scope.productRegexDoEdit=function(o){
    if(o.xpathRegex==''||o.id==''){
      alert("请输入xpath值");
      return ;
    }
    outProductService.editProductRegex(o).then(function(json){
      $route.reload();
    });
  }

}]);

